package com.starbaba.system.roleofmenu.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.HibernateDAO;
import com.starbaba.core.hibernate.Strategy;
import com.starbaba.system.menu.model.MenuVO;
import com.starbaba.system.roleofmenu.model.RoleOfMenuVO;
/**
 * 查询角色菜单列表信息
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class QueryRoleOfMenuStrategy implements Strategy{
	
	private String roleId;
	
	public QueryRoleOfMenuStrategy(String roleId){
		this.roleId = roleId;
	}

	@Override
	@SuppressWarnings("unchecked")
	public Object process() throws Exception {
		/**首先查询角色菜单列表信息,需要保证菜单ID不是父菜单 **/
		StringBuffer roleofmenuHQL = new StringBuffer();
		roleofmenuHQL.append(" SELECT roleofmenu FROM ");
		roleofmenuHQL.append(RoleOfMenuVO.class.getName()).append(" roleofmenu ");
		roleofmenuHQL.append(" WHERE roleId = ? ");
		
		HibernateDAO roleOfMenuDAO = DAOFactory.getInstance().createHibernateDAO(RoleOfMenuVO.class);
		HibernateDAO menuDAO = DAOFactory.getInstance().createHibernateDAO(MenuVO.class);
		
		/**
		 * 查询角色菜单信息
		 */
		List<RoleOfMenuVO> roleOfMenuList = (List<RoleOfMenuVO>) roleOfMenuDAO.findByHQL(roleofmenuHQL.toString(), new Object[]{new Long(getRoleId())});
		
		Map<String, Long> roleOfMenuMap = new HashMap<String,Long>();
		for(RoleOfMenuVO roleOfMenuVO : roleOfMenuList){
			roleOfMenuMap.put(roleOfMenuVO.getMenuId(), roleOfMenuVO.getRoleId());
		}
		roleOfMenuList.clear();
		roleOfMenuList = null;
		
		List<MenuVO> menuList = (List<MenuVO>) menuDAO.findAll();
		for(MenuVO menuVO : menuList){
			if(roleOfMenuMap.containsKey(menuVO.getControlid())){
				menuVO.setChecked(true);
			}
		}
		return menuList;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

}
